package com.zfl.thread.day01;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


class TicketThread implements Callable<Integer> {
    private int ticketNum=100;


    @Override
    public Integer call() throws Exception {
        while(ticketNum>0) {
            Thread.sleep(100);
            this.ticketNum--;
            System.out.println(Thread.currentThread().getName()+"卖出一站票，还剩["+this.ticketNum+"]张");

        }
        return ticketNum;
    }
}

public class DemoFutureTask {

    /**
     *  每一个线程是同样的逻辑，都是抢票
     *  不同的FutureTask共用一个Callable
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> ticketThread = new TicketThread();
        FutureTask<Integer> futureTask = new FutureTask<>(ticketThread);
        new Thread(futureTask).start();
        new Thread(new FutureTask<Integer>(ticketThread) ).start();
        new Thread(new FutureTask<Integer>(ticketThread) ).start();
        new Thread(new FutureTask<Integer>(ticketThread) ).start();
        System.out.println("结果:"+futureTask.get());
    }
}
